package com.zc.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zc.dao.IElecDao;
import com.zc.domain.DoubleListData;
import com.zc.domain.Item;
import com.zc.service.ElecService;

@Service(value="elecService")
public class ElecServiceImpl implements ElecService{
	@Autowired
	private IElecDao elecDao;
	
	@Override
	public DoubleListData getElec() {
		List<Item> mainElec=elecDao.getElec("mtsjgd_live");
		List<Item> subElec=elecDao.getElec("ftsjgd_live");
		for(Item i:mainElec) {
			i.setTimeStamp(i.getTimeStamp().substring(11, 13));
		}
		for(Item i:subElec) {
			i.setTimeStamp(i.getTimeStamp().substring(11, 13));
		}
		
		return new DoubleListData(mainElec,subElec);
	}
	
	@Override
	public DoubleListData getTodayElec() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date=df.format(new Date());// new Date()为获取当前系统时间
        //date=date+" 00:00:00";
		List<Item> mainElec=elecDao.getTodayElec("mtsjgd_live",date);
		List<Item> subElec=elecDao.getTodayElec("ftsjgd_live",date);
		for(Item i:mainElec) {
			i.setTimeStamp(i.getTimeStamp().substring(11, 13));
		}
		for(Item i:subElec) {
			i.setTimeStamp(i.getTimeStamp().substring(11, 13));
		}		
		return new DoubleListData(mainElec,subElec);
	}
	
	@Override
	public DoubleListData getMonthElec() {
		Calendar cal = Calendar.getInstance();
		int month = cal.get(Calendar.MONTH) + 1;
		int year = cal.get(Calendar.YEAR);
		String nowMonth=month<10?"0"+month:""+month;
		String nextMonth=month+1<10?"0"+(month+1):""+(month+1);
		String beginDate=year+"-"+nowMonth+"-01 00:00:00";
		String nextYear=month!=12?year+"":year+1+"";
		String endDate=nextYear+"-"+nextMonth+"-01 00:00:00";
		
		List<Item> mainElec=elecDao.getMonthElec("mtsjgdtj",beginDate,endDate);
		List<Item> subElec=elecDao.getMonthElec("ftsjgdtj",beginDate,endDate);
		for(Item i:mainElec) {
			i.setTimeStamp(i.getTimeStamp().substring(8, 10));
		}
		for(Item i:subElec) {
			i.setTimeStamp(i.getTimeStamp().substring(8, 10));
		}		
		return new DoubleListData(mainElec,subElec);
	}
	
	@Override
	public DoubleListData getYearElec() {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		String date=year+"-01-01 00:00:00";
		
		List<Item> mainElec=elecDao.getYearElec("mtsjgdtj",date);
		List<Item> subElec=elecDao.getYearElec("ftsjgdtj",date);
		for(Item i:mainElec) {
			i.setTimeStamp(i.getTimeStamp().substring(5, 7));
		}
		for(Item i:subElec) {
			i.setTimeStamp(i.getTimeStamp().substring(5, 7));
		}		
		return new DoubleListData(mainElec,subElec);
	}

	@Override
	public DoubleListData daySelect(String beginDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
		Date date=null;
		try {
			date=sdf.parse(beginDate);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        c.add(Calendar.DAY_OF_MONTH, 1);
        String endDate=sdf.format(c.getTime());
        System.out.println(endDate);
        
        
        List<Item> mainElec=elecDao.select("mtsjgd_live", beginDate, endDate);
        List<Item> subElec=elecDao.select("ftsjgd_live", beginDate, endDate);
       
        for(Item i:mainElec) {
			i.setTimeStamp(i.getTimeStamp().substring(11, 13));
		}
		for(Item i:subElec) {
			i.setTimeStamp(i.getTimeStamp().substring(11, 13));
		}		
        
        
		return new DoubleListData(mainElec,subElec);
	}

	@Override
	public DoubleListData monthSelect(String beginDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); 
		Date date=null;
		try {
			date=sdf.parse(beginDate);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        c.add(Calendar.MONTH, 1);
        String endDate=sdf.format(c.getTime());
        System.out.println(endDate);
        
        
        List<Item> mainElec=elecDao.select("mtsjgdtj", beginDate, endDate);
        List<Item> subElec=elecDao.select("ftsjgdtj", beginDate, endDate);
       
        for(Item i:mainElec) {
			i.setTimeStamp(i.getTimeStamp().substring(8, 10));
		}
		for(Item i:subElec) {
			i.setTimeStamp(i.getTimeStamp().substring(8, 10));
		}		
        
        
		return new DoubleListData(mainElec,subElec);
	}

	@Override
	public DoubleListData yearSelect(String beginDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); 
		Date date=null;
		try {
			date=sdf.parse(beginDate);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        c.add(Calendar.YEAR, 1);
        String endDate=sdf.format(c.getTime());
        
        List<Item> mainElec=elecDao.yearSelect("mtsjgdtj", beginDate, endDate);
        List<Item> subElec=elecDao.yearSelect("ftsjgdtj", beginDate, endDate);
       
        for(Item i:mainElec) {
			i.setTimeStamp(i.getTimeStamp().substring(5, 7));
		}
		for(Item i:subElec) {
			i.setTimeStamp(i.getTimeStamp().substring(5, 7));
		}		
		return new DoubleListData(mainElec,subElec);
	}

	
}
